/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.entity.Departments;
import bean.entity.Employees;
import bean.entity.Vacances;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Vu Thang
 */
@Stateless
@LocalBean
public class VacancySessionBean {

    @PersistenceContext(unitName = "RecruitmentProcessSystem-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public List<Vacances> findAllVacances() {
        return em.createNamedQuery("Vacances.findAll").getResultList();
    }

    public List<Vacances> findVacacyTitle(String title) {
        List<Vacances> list = null;
        try {
            Query query = em.createNamedQuery("Vacances.findByVacantyTitle");
            query.setParameter(1, "%" + title + "%");

            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }

    public List<Vacances> findIdVacancy(String id) {
        List<Vacances> list = null;
        try {
            Query query = em.createNamedQuery("Vacances.findByIDVacancy");
            query.setParameter(1, "%" + id + "%");

            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }

    public boolean addNewVacancy(Vacances es) {
        try {
            em.persist(es);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public Vacances findVacancyByID(int vacancyID) {
        Vacances va = null;
        try {
            Query query = em.createNamedQuery("Vacances.findByVacancyID");
            query.setParameter("vacancyID", vacancyID);
            va = (Vacances) query.getSingleResult();

        } catch (NoResultException e) {
            return null;
        }
        return va;
    }

    public void updateVacancy(List<Vacances> List) {
        for (Vacances a : List) {
            em.merge(a);
        }
    }

    public List<Vacances> findVacancyByDepartID(String departID) {
        List<Vacances> vacancesList = null;
        try {
            Query query = em.createNamedQuery("Vacances.findByDepartmentID");
            query.setParameter("departID", departID);
            vacancesList = query.getResultList();
            return vacancesList;
        } catch (NoResultException e) {
            return vacancesList;
        }
    }
//    public List<Vacances> pagingVacacesTable(int currPage,int recodperpage)
//    {
//        List<Vacances> list = null;
//
//        try {
//            Query query = em.createNamedQuery("Vacances.paging");
////            query.setParameter("currPage", currPage);
////            query.setParameter("recodperpage", recodperpage);
//            list = query.getResultList();
//        } catch (NoResultException e) {
//            return null;
//        }
//        return list;
//    }
}
